<?php
namespace App\Controllers;

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use App\Models\RoleModel;
use stdClass;
date_default_timezone_set('prc');
class RoleController extends BaseController {

	public function RoleList($request, $response, $args) {
		include APP_PATH . '/Views/Role/list.html';
	}

	public function RoleInfo($request, $response, $args) {
		$Rolename=$request -> getParam('rolename');
		$user = new RoleModel();
		$result=$user -> Roleselect($Rolename);
		
		$draw=$request->getParam('draw')*1;//第几次请求
		$start=$request->getParam('start');//第一条数据开始的索引
		$length=$request->getParam('length');//返回的数据长度
		$newarr = new stdClass();
		$newarr->draw = $draw;
		$newarr->page = count($result)/$length;
		$newarr->recordsTotal = count($result);
		$newarr->recordsFiltered = count($result);
		$newarr->data = array();
		$h=0;
		for($i = $start; $i<$start+$length;$i++,$h++)
			{   if(!empty($result[$i])){
				$newarr->data[$h] = $result[$i];
				}else{
					break;
				}
						
			}
				return $newResponse = $response->withJson($newarr, 200);
		
	}
	
	public function RoleDelete($request, $response, $args) {
		$id = $request -> getParam('id');
		$user = new RoleModel();
		$user -> delete($id);
		echo json_encode([
            'code' => 0,
            'msg' => 'success'
        ]);
	}

	public function RoleUpdate($request, $response, $args) {
		$username = $request -> getParam('name');
		$id = $request -> getParam('id');
		$time=date('Y-m-d H:i:s',time());
		$user = new RoleModel();
		$data = array('RoleName' =>$username,'Time'=>$time,);
		$user -> update($id,$data);
		echo json_encode([
            'code' => 0,
            'msg' => 'success'
        ]);
	}
	public function RoleAdd($request, $response, $args) {
		$rolename = $request -> getParam('name');
		$time=date('Y-m-d H:i:s',time());
		$user = new RoleModel();
		$data = array('rolename' =>$rolename,'Time'=>$time,);
		$user -> add($data);
		echo json_encode([
            'code' => 0,
            'msg' => 'success'
        ]);
	}
	public function RoleItem($request, $response, $args) {
		$user = new RoleModel();
		$user -> RoleItem();
	}
}


